package com.buba.springboot.filemanager.service.Impl;

import com.buba.springboot.filemanager.entity.fileManager;
import com.buba.springboot.filemanager.dao.FileManagerMapper;
import com.buba.springboot.filemanager.service.FileManagerService;
import com.buba.springboot.utils.CommonResult;
import com.buba.springboot.utils.FileUplode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
public class FileManagerImpl implements FileManagerService {

    @Autowired
    private FileManagerMapper fileManagerMapper;

    @Autowired
    private FileUplode fileUplode;

    @Override
    public CommonResult uploadFile(MultipartFile multipartFile) {
        //创建实体类对象
        fileManager fileManager =new fileManager();
        //调用文件上传工具类获取文件信息
        CommonResult commonResult = fileUplode.fileInfo(multipartFile);
        //将文件信息复制到对象中
        BeanUtils.copyProperties(commonResult.getObj(),fileManager);
        //复制完对象信息以后，将带对象信息的接口返回给mapper，并传到数据库中去
         fileManagerMapper.savaFileInfo(fileManager);
        System.out.println("11111");
        //如果mapper有参数，那么就会通过工具类以流的形式上传文件，并放到指定路径，否则就上传失败
        if(fileManager!=null){
            System.out.println("222222222222222");
            fileUplode.fileUplode(fileManager.getFilePath(),multipartFile);
        }else{
            CommonResult.fail("文件上传失败");
        }
        //返回主键
        return CommonResult.ok(fileManager.getId());
    }
}





